package com.hs.jhzd.controller;

import com.hs.jhzd.entity.JDWPBGMX;
import com.hs.jhzd.entity.WPJSZPB;
import com.hs.jhzd.entity.dto.JDWPBGXXDto;
import com.hs.jhzd.repository.ArticleRepository;
import com.hs.jhzd.repository.JDWPBGMXRepository;
import com.hs.jhzd.repository.WpjszpbRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Blob;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleRepository repository;
    @Autowired
    private WpjszpbRepository wpjszpbRepository;
    @Autowired
    private JDWPBGMXRepository jdwpbgmxRepository;

    @RequestMapping("/find")
    @ResponseBody
    public List<JDWPBGXXDto> find(String rybh) {
        List<JDWPBGXXDto> all = repository.findByRybh(rybh);
        for (int i = 0; i < all.size(); i++) {
            JDWPBGXXDto jdwpbgxx = all.get(i);
            List<JDWPBGMX> jdwpbgmxList = jdwpbgmxRepository.findByBgzybh(jdwpbgxx.getZybh());
            List<WPJSZPB> wpjszpbList = wpjszpbRepository.findByWpjszybh(jdwpbgxx.getZybh());
            jdwpbgxx.setJdwpbgmxList(jdwpbgmxList);
            jdwpbgxx.setWpjszpbList(wpjszpbList);
        }
        return all;
    }

    @RequestMapping("/sign")
    @ResponseBody
    public boolean findByJsbh(String rybh, String djbh) {
        repository.updateByRybhandDjbh(rybh, djbh);
        return true;
    }

    @RequestMapping("/findZpById")
    @ResponseBody
    public WPJSZPB findByJsbh(String id) {
        Optional<WPJSZPB> wpjszpb = wpjszpbRepository.findByZpzybh(id);
        if (wpjszpb.isPresent()) {
            WPJSZPB wpjszpb1 = wpjszpb.get();
            return wpjszpb1;
        }
        return null;
    }

    @RequestMapping(value = {"/findPhotoById"})
    @ResponseBody
    public void packagesPhoto(HttpServletRequest request, HttpServletResponse response, String id) throws Exception {

        Optional<WPJSZPB> wpjszpb = wpjszpbRepository.findByZpzybh(id);
        InputStream result = null;
        if (wpjszpb.isPresent()) {
            byte[] ByteArr = blobToBytes(wpjszpb.get().getZp());
            response.setContentType("image/jpeg ");
            OutputStream output = response.getOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(ByteArr);// 获取实体类对应Byte
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                output.write(buf, 0, len);
            }
            output.flush();
            output.close();
        }

    }


    private byte[] blobToBytes(Blob blob) {
        BufferedInputStream is = null;
        try {
            is = new BufferedInputStream(blob.getBinaryStream());
            byte[] bytes = new byte[(int) blob.length()];
            int len = bytes.length;
            int offset = 0;
            int read = 0;
            while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
                offset += read;
            }
            return bytes;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                return null;
            }
        }

    }


}
